part of '../repository.dart';

const _kLearningIntervalStartTimeKey = 'start_time';
const _kLearningIntervalEndTimeKey = 'end_time';

class _LearningIntervalDto extends Equatable {
  const _LearningIntervalDto._internal({
    required this.startTimeDto,
    required this.endTimeDto,
  });

  final _TimeDto startTimeDto;
  final _TimeDto endTimeDto;

  @override
  List<Object?> get props {
    return [
      startTimeDto,
      endTimeDto,
    ];
  }

  factory _LearningIntervalDto.fromJson(Map<String, dynamic> json) {
    final startTimeString = json[_kLearningIntervalStartTimeKey] as String;
    final startTimeDto = _TimeDto.fromString(startTimeString);

    final endTimeString = json[_kLearningIntervalEndTimeKey] as String;
    final endTimeDto = _TimeDto.fromString(endTimeString);

    return _LearningIntervalDto._internal(
      startTimeDto: startTimeDto,
      endTimeDto: endTimeDto,
    );
  }

  factory _LearningIntervalDto.fromEntity(LearningInterval learningInterval) {
    final startTimeDto = _TimeDto.fromEntity(learningInterval.startTime);
    final endTimeDto = _TimeDto.fromEntity(learningInterval.endTime);

    return _LearningIntervalDto._internal(
      startTimeDto: startTimeDto,
      endTimeDto: endTimeDto,
    );
  }

  Map<String, dynamic> toJson() {
    return {
      _kLearningIntervalStartTimeKey: startTimeDto.toString(),
      _kLearningIntervalEndTimeKey: endTimeDto.toString(),
    };
  }

  LearningInterval toEntity() {
    return LearningInterval(
      startTime: startTimeDto.toEntity(),
      endTime: endTimeDto.toEntity(),
    );
  }
}
